package com.scaffold.authority.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.scaffold.authority.entity.Authority;

import wangjg.commons.page.Page;

@Repository
public class AuthorityDAO extends BasicBizDAO<Authority, Long> {

	public List<Authority> pageQuery(Page page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

	public List<Authority> pageQuery(Page page, Integer type, String name,
			String displayName, String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if (type != null) {
			criteria.add(Restrictions.eq("type", type));
		}
		if (name != null) {
			criteria.add(Restrictions.eq("name", name));
		}
		if (displayName != null) {
			criteria.add(Restrictions.eq("displayName", displayName));
		}
		if (keyword != null && !keyword.isEmpty()) {
			LogicalExpression expr = Restrictions.or(Restrictions.like("name",
					keyword, MatchMode.ANYWHERE), Restrictions.like(
					"displayName", keyword, MatchMode.ANYWHERE));
			criteria.add(expr);
		}
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

}
